package presentacion;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

import java.awt.Window.Type;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;

import dominio.ListenerCerrarVentana;
import dominio.Usuario;
import dominio.Util;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;

public class Login {

	private JFrame VentanaLogin;
	private JPanel pnlLogin;
	private JLabel lblUsuario;
	private JLabel lblContrasea;
	private JButton btnEntrar;
	private JTextField txtUsuario;
	private JPasswordField pssContrasena;
	private JButton btnIdioma;
	private JButton btnAyuda;
	private Border bordeRojo = BorderFactory.createLineBorder(Color.RED);
	private Border bordeVerde = BorderFactory.createLineBorder(Color.GREEN);
	private JLabel lblCheck;
	static Date fecha;
	private Usuario u;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.VentanaLogin.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * 
	 * @throws Exception
	 */
	public Login() throws Exception {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * 
	 * @throws Exception
	 */
	private void initialize() throws Exception {
		VentanaLogin = new JFrame();
		VentanaLogin.addWindowListener(new ListenerCerrarVentana(VentanaLogin));
		VentanaLogin.setType(Type.UTILITY);
		VentanaLogin.setResizable(false);
		VentanaLogin.setTitle(Messages.getString("Login.0")); //$NON-NLS-1$
		VentanaLogin.setBounds(100, 100, 241, 235);
		VentanaLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		VentanaLogin.setLocationRelativeTo(null);
		{
			pnlLogin = new JPanel();
			VentanaLogin.getContentPane().add(pnlLogin, BorderLayout.CENTER);
			pnlLogin.setLayout(null);

			{
				btnIdioma = new JButton(Messages.getString("Login.1")); //$NON-NLS-1$
				btnIdioma.setMnemonic('L');
				btnIdioma.setBounds(142, 0, 54, 21);
				pnlLogin.add(btnIdioma);
				btnIdioma.addActionListener(new btnESPActionListener());
				btnIdioma.setCursor(Cursor
						.getPredefinedCursor(Cursor.HAND_CURSOR));
				btnIdioma.setBorder(new CompoundBorder());
				btnIdioma.setPreferredSize(new Dimension(31, 20));
				btnIdioma.setAutoscrolls(true);
				btnIdioma.setIcon(new ImageIcon(Login.class
						.getResource(Messages.getString("Login.2")))); //$NON-NLS-1$
				btnIdioma.setMinimumSize(new Dimension(40, 20));
				btnIdioma.setMaximumSize(new Dimension(40, 20));
				btnIdioma.setFont(new Font("Tahoma", Font.PLAIN, 11)); //$NON-NLS-1$
				btnIdioma.setToolTipText(Messages.getString("Login.4")); //$NON-NLS-1$
			}
			{
				btnAyuda = new JButton(""); //$NON-NLS-1$
				btnAyuda.setMnemonic('H');
				btnAyuda.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						JOptionPane.showMessageDialog(
								VentanaLogin,
								Messages.getString("Login.3"), Messages.getString("Login.5"), //$NON-NLS-1$ //$NON-NLS-2$
								JOptionPane.INFORMATION_MESSAGE);
					}
				});
				btnAyuda.setCursor(Cursor
						.getPredefinedCursor(Cursor.HAND_CURSOR));
				btnAyuda.setToolTipText(Messages.getString("Login.6")); //$NON-NLS-1$
				btnAyuda.setBounds(200, 0, 23, 21);
				btnAyuda.setBorder(new CompoundBorder());
				btnAyuda.setAutoscrolls(true);
				btnAyuda.setAlignmentX(Component.CENTER_ALIGNMENT);
				btnAyuda.setFont(new Font("Tahoma", Font.PLAIN, 8)); //$NON-NLS-1$
				btnAyuda.setHorizontalAlignment(SwingConstants.TRAILING);
				btnAyuda.setIcon(new ImageIcon(Login.class
						.getResource("/recursos/help.png"))); //$NON-NLS-1$
				pnlLogin.add(btnAyuda);
			}
			{
				lblUsuario = new JLabel(Messages.getString("Login.9")); //$NON-NLS-1$
				lblUsuario.setBounds(25, 34, 88, 16);
				pnlLogin.add(lblUsuario);
			}
			{

				txtUsuario = new JTextField();
				txtUsuario.setToolTipText(Messages.getString("Login.10")); //$NON-NLS-1$
				txtUsuario.setBounds(25, 55, 171, 22);
				pnlLogin.add(txtUsuario);
				txtUsuario.setColumns(10);
				txtUsuario.addFocusListener(new MiFocusListener());
				txtUsuario.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent arg0) {
						try {
							u = new Usuario(txtUsuario.getText(), null, null);
							u.leerUsuario();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						if (u.getContrasena() != null) { //$NON-NLS-1$
							txtUsuario.setBorder(bordeVerde);
							lblCheck.setIcon(new ImageIcon(Login.class
									.getResource("/recursos/tick.png"))); //$NON-NLS-1$
							pssContrasena.setEnabled(true);
							pssContrasena.setToolTipText(Messages
									.getString("Login.8")); //$NON-NLS-1$

						} else {
							txtUsuario.setBorder(bordeRojo);
							lblCheck.setIcon(new ImageIcon(Login.class
									.getResource("/recursos/cross.png"))); //$NON-NLS-1$
							pssContrasena.setEnabled(false);
							pssContrasena.setToolTipText(Messages
									.getString("Login.11")); //$NON-NLS-1$
						}
					}
				});

			}
			{
				lblContrasea = new JLabel(Messages.getString("Login.14")); //$NON-NLS-1$
				lblContrasea.setBounds(25, 82, 111, 16);
				pnlLogin.add(lblContrasea);
			}

			{
				pssContrasena = new JPasswordField();
				pssContrasena.addKeyListener(new KeyAdapter() {
					@Override
					public void keyPressed(KeyEvent arg0) {
						if (pssContrasena.isEnabled() == true) {
							btnEntrar.setEnabled(true);
						}
						if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
							pssContrasena.addActionListener(new EntrarActionListener());

						}
					}
				});
				pssContrasena.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						if (pssContrasena.isEnabled() == true) {
							btnEntrar.setEnabled(true);
						}
					}
				});
				pssContrasena.setToolTipText(Messages
						.getString("Login.pssContrasena.toolTipText")); //$NON-NLS-1$
				pssContrasena.setBounds(25, 103, 171, 22);
				pssContrasena.setEnabled(false);
				pnlLogin.add(pssContrasena);
			}
			{
				btnEntrar = new JButton(Messages.getString("Login.15")); //$NON-NLS-1$
				btnEntrar.setEnabled(false);
				btnEntrar.setIcon(new ImageIcon(Login.class
						.getResource("/recursos/IconoEntrar-32.png"))); //$NON-NLS-1$
				btnEntrar.setCursor(Cursor
						.getPredefinedCursor(Cursor.HAND_CURSOR));
				btnEntrar.setToolTipText(Messages.getString("Login.17")); //$NON-NLS-1$
				btnEntrar.addActionListener(new EntrarActionListener());
				btnEntrar.setBounds(25, 130, 171, 41);
				pnlLogin.add(btnEntrar);
			}
			{
				lblCheck = new JLabel(""); //$NON-NLS-1$
				lblCheck.setBounds(200, 58, 23, 16);
				pnlLogin.add(lblCheck);
			}
		}
	}

	@SuppressWarnings("deprecation")
	public static String getFecha() {
		String horas, minutos, segundos;
		horas = Integer.toString(fecha.getHours());
		minutos = Integer.toString(fecha.getMinutes());
		segundos = Integer.toString(fecha.getSeconds());
		if (horas.length() == 1)
			horas = "0" + horas; //$NON-NLS-1$
		if (minutos.length() == 1)
			minutos = "0" + minutos; //$NON-NLS-1$
		if (segundos.length() == 1)
			segundos = "0" + segundos; //$NON-NLS-1$
		return fecha.getDate()
				+ "/" + Integer.toString(fecha.getMonth() + 1) + "/" + Integer.toString(fecha.getYear() + 1900) + " " + horas + ":" + minutos + ":" + segundos; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

	}
	private class EntrarActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				if (u.getContrasena().equals(
						pssContrasena.getText())) {
					Menu gc = new Menu();
					gc.VentanaPrincipal.setVisible(true);
					gc.lblNombre.setText(txtUsuario.getText());
					VentanaLogin.setVisible(false);
					fecha = new Date();
					if (u.getConexion() == null) {
						u.setConexion("-- / -- / --  --:--:--"); //$NON-NLS-1$
					}
					gc.lblHora.setText(u.getConexion());
					gc.lblUserIdent.setText(txtUsuario.getText());
					u.setConexion(getFecha());
					u.modificar();

				} else {
				
					JOptionPane.showMessageDialog(VentanaLogin,
							Messages.getString("Login.21")); //$NON-NLS-1$
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private class MiFocusListener extends FocusAdapter {
		@Override
		public void focusGained(FocusEvent e) {
			e.getComponent().setBackground(new Color(250, 250, 210));
		}

		@Override
		public void focusLost(FocusEvent e) {
			e.getComponent().setBackground(new Color(250, 250, 250));
		}
	}

	private class btnESPActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (btnIdioma.getText().equals("ESP")) { //$NON-NLS-1$
			
				Messages.setIdioma("inglés"); //$NON-NLS-1$
				Util.internacionalizarMensajes("Inglés");
			} else {
				Util.internacionalizarMensajes("castellano");
				Messages.setIdioma("castellano"); //$NON-NLS-1$
				
			}
			Login ventana = null;
			try {
				ventana = new Login();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			ventana.VentanaLogin.setVisible(true);
			VentanaLogin.setVisible(false);
		}
	}

}